package bookstore_mall.controller.management;

import bookstore_mall.common.DeleteRequest;
import bookstore_mall.common.PageVO;
import bookstore_mall.domain.BookCategory;
import bookstore_mall.resp.ResponseResult;
import bookstore_mall.service.BookCategoriesService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import bookstore_mall.domain.dto.category.updateCategoryRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags = "图书分类管理")
@RequestMapping("/management/bookCategories")
public class BookCategoriesController {
    
    @Autowired
    private BookCategoriesService bookCategoryService;

    /**
     * 获取所有类别
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param name     名称
     * @return 响应结果<ipage < 图书分类>>
     */
    @GetMapping
    @ApiOperation(value = "获取所有类别")
    public ResponseResult<PageVO<BookCategory>> getAllCategories(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String name) {
        return ResponseResult.success(bookCategoryService.findByPage(page, pageSize, name));
    }

    /**
     * 按id获取类别
     *
     * @param categoryId 类别id
     * @return 响应结果<图书类别>
     */
    @GetMapping("/getByCategoryId")
    public ResponseResult<BookCategory> getCategoryById(String categoryId) {
        return ResponseResult.success(bookCategoryService.findByCategoryId(categoryId));
    }

    /**
     * 按父id获取类别
     *
     * @param parentId 父id
     * @param page     页面
     * @param pageSize 页面大小
     * @return 响应结果<ipage < 图书分类>>
     */
    @GetMapping("/parent/{parentId}")
    public ResponseResult<IPage<BookCategory>> getCategoriesByParentId(
            @PathVariable Integer parentId,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        return ResponseResult.success(bookCategoryService.findByParentId(parentId, page, pageSize));
    }

    /**
     * 创建类别
     *
     * @param category 类别
     * @return 响应结果<图书类别>
     */
    @PostMapping
    @ApiOperation(value = "创建类别")
    public ResponseResult<BookCategory> createCategory(@RequestBody BookCategory category) {
        return ResponseResult.success(bookCategoryService.create(category));
    }

    /**
     * 更新类别
     *
     * @param category   类别
     * @return 响应结果<boolean>
     */
    @PostMapping ("/updateCategoryId")
    @ApiOperation(value = "更新类别")
    public ResponseResult<Boolean> updateCategory(String categoryId,@RequestBody updateCategoryRequest category) {
        return ResponseResult.success(bookCategoryService.updateCategory(category, categoryId));
    }

    /**
     * 删除类别
     *
     * @return 响应结果<boolean>
     */
    @PostMapping("/deleteByCategoryId")
    @ApiOperation(value = "删除类别")
    public ResponseResult<Boolean> deleteCategory(@RequestBody DeleteRequest deleteRequest) {
        String categoryId = deleteRequest.getId().toString();
        return ResponseResult.success(bookCategoryService.deleteCategory(categoryId));
    }
}
